package bod;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Images {

	// res.img.blocks
	public static BufferedImage[]	aire;
	public static BufferedImage[]	erth;
	public static BufferedImage[]	gras;
	public static BufferedImage[]	sand;
	public static BufferedImage[]	skyb;
	public static BufferedImage[]	stne;
	public static BufferedImage[]	tnnl;
	public static BufferedImage[]	watr;
	// res.img.gui
	public static BufferedImage		btnn;
	public static BufferedImage		btnp;
	public static BufferedImage		crsr;
	public static BufferedImage[]	hlth;
	public static BufferedImage		prge;
	public static BufferedImage		prgf;
	// res.img.maps
	public static BufferedImage		act1;
	public static BufferedImage		act2;
	public static BufferedImage		act3;
	// res.img.objs
	public static BufferedImage		hous;
	public static BufferedImage[]	plyr;
	public static BufferedImage		rain;
	public static BufferedImage		snow;
	public static BufferedImage[]	trch;

	public static void initialize() {
		// res.img.blocks
		aire = load("/img/blocks/aire.png", 16, 16);
		erth = load("/img/blocks/erth.png", 16, 16);
		gras = load("/img/blocks/gras.png", 16, 16);
		sand = load("/img/blocks/sand.png", 16, 16);
		skyb = load("/img/blocks/skyb.png", 16, 16);
		stne = load("/img/blocks/stne.png", 16, 16);
		tnnl = load("/img/blocks/tnnl.png", 16, 16);
		watr = load("/img/blocks/watr.png", 16, 16);
		// res.img.gui
		btnn = load("/img/gui/btnn.png");
		btnp = load("/img/gui/btnp.png");
		crsr = load("/img/gui/crsr.png");
		hlth = load("/img/gui/hlth.png", 256, 128);
		prge = load("/img/gui/prge.png");
		prgf = load("/img/gui/prgf.png");
		// res.img.maps
		act1 = load("/img/maps/act1.png");
		act2 = load("/img/maps/act2.png");
		act3 = load("/img/maps/act3.png");
		// res.img.objs
		hous = load("/img/objs/hous.png");
		plyr = load("/img/objs/plyr.png", 32, 48);
		rain = load("/img/objs/rain.png");
		snow = load("/img/objs/snow.png");
		trch = load("/img/objs/trch.png", 8, 16);
	}

	private static BufferedImage[] load(String imgName, int width, int height) {
		BufferedImage source = load(imgName);
		int xTiles = source.getWidth() / width;
		int yTiles = source.getHeight() / height;
		BufferedImage[] result = new BufferedImage[xTiles * yTiles];
		for (int x = 0; x < xTiles; x++) {
			for (int y = 0; y < yTiles; y++) {
				result[x + y * xTiles] = source.getSubimage(x * width, y
						* height, width, height);
			}
		}
		return result;
	}

	private static BufferedImage load(String imgName) {
		BufferedImage img = null;
		try {
			img = ImageIO.read(Images.class.getResource(imgName));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return img;
	}

}